package com.example.wificov;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.database.ListScan;
import com.example.database.MyArrayAdapter;
import com.example.database.myDBHelper;
import com.example.wificovv2.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class Wlanpicture extends Activity {

myDBHelper db;
	
	LinearLayout mLinearLayout;
	
	Paint paint1 = new Paint();
	
	Paint paint2 = new Paint();
	
	Paint paint3 = new Paint();
	
	int height = 0, width = 0, imgheight = 0, imgwidth=0;
	
	float ratio1 = 0f, ratio2 = 0f;
	
	Resources res;

	Bitmap bm;
	
	BitmapDrawable bd;
	ImageView picture;
	ArrayList<ListScan>arrListScan=new ArrayList<ListScan>();
	//Sử dụng MyArrayAdapter thay thì ArrayAdapter
	MyArrayAdapter adapter=null;
	ListView lvssid;
	EditText filepowname;
	
	//Cac thu vien muc mau can thiet
	int level1, level2, level3, level4, level5, level6, level7, level8, level9;
	int level10, level11, level12, level13, level14, level15;
	//
	Button loadpow, loadbw, save, finish;
	Canvas c;
	String value = "";
	int APID;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wlanpicture);
		//Initilize ImageView to contain picture
		level1 = getResources().getColor(R.color.level1);
		level2= getResources().getColor(R.color.level2);
		level3= getResources().getColor(R.color.level3);
		level4= getResources().getColor(R.color.level4);
		level5= getResources().getColor(R.color.level5);
		level6= getResources().getColor(R.color.level6);
		level7= getResources().getColor(R.color.level7);
		level8= getResources().getColor(R.color.level8);
		level9= getResources().getColor(R.color.level9);
		level10= getResources().getColor(R.color.level10);
		level11= getResources().getColor(R.color.level11);
		level12= getResources().getColor(R.color.level12);
		level13= getResources().getColor(R.color.level13);
		level14= getResources().getColor(R.color.level14);
		level15= getResources().getColor(R.color.level15);
		
		db = new myDBHelper(this);
	    mLinearLayout = new LinearLayout(this);
	    picture = (ImageView) findViewById(R.id.picwlan);
	    loadpow = (Button) findViewById(R.id.wlanpowpic);
	    loadbw = (Button) findViewById(R.id.wlanbandpic);
	    save = (Button) findViewById(R.id.savepicture);
	    filepowname = (EditText) findViewById(R.id.filenamewlanpic);
	    finish = (Button) findViewById(R.id.backfunc);
	    lvssid = (ListView) findViewById(R.id.listap);
	    
	  //1. Khởi tạo dữ liệu cho mảng arr 
	    List<String> name = db.getAPName(APID);
	    String[] apName = new String[name.size()];
	    for (int i =0; i< name.size(); i++)
	    	apName[i] = name.get(i);
		final String []  arr = new String[name.size()]; 
//		name.toArray(arr);
//	  		 final String arr[]={"1","2","3","4","5","6","4","4","4","4","4","4"};
	  	MyArrayAdapter adapter=new MyArrayAdapter(this, R.layout.my_item_layout, apName);
	  	//4. Đưa Data source vào ListView
	  	lvssid.setAdapter(adapter);
	    
	    lvssid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			 public void onItemClick(
			 AdapterView<?> parent, View view, int position, long id) {
				 String key = "";
				 HashMap<String, String> item = (HashMap<String, String>) parent.getAdapter().getItem(position);
				 for (HashMap.Entry<String,String> entry : item.entrySet()) {
					// key = entry.getKey();
					 value = entry.getValue();					
					}
			 }
			 });
	    finish.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				System.exit(0);
			}
		});
	    
	    
	    loadpow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Xét listview được chọn là ssid nào 
				//Lấy dữ liệu về powlevel từ các ssid được chọn đưa vào mảng data1 hoặc 2
//				APID = db.getAPIdByName(value);	
				
				res = getResources();
				    //Get image's size
				    BitmapFactory.Options dimensions = new BitmapFactory.Options(); 
				    dimensions.inJustDecodeBounds = true;
				    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.newmap, dimensions);
				    imgheight = dimensions.outHeight;
				    imgwidth =  dimensions.outWidth;
				    
				    //calculate the ratios between image's size and real world distance - how many pixels per meter
//				    String[] size = db.getMapsize(mapId);
//					ratio1 = (float)imgwidth/(Integer.parseInt(size[0]));
//				    ratio2 = (float)imgheight/(Integer.parseInt(size[1]));
				    ratio1 = (float)imgwidth/160;
				    ratio2 = (float)imgheight/100;
				    
				    //cast from immutableBitmap to mutableBitmap to insert into Canvas 
				    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.newmap );
				    Bitmap mutableBitmap = bm.copy(Bitmap.Config.ARGB_8888, true);
				    Canvas c = new Canvas( mutableBitmap );
				   
				    //set attributes for Paints (used to draw)
				    paint1.setColor( Color.BLUE );	    
				    paint2.setColor( Color.RED );
				    paint2.setStrokeWidth( 2f );   
				    paint3.setColor( Color.GREEN );
				    paint3.setStrokeWidth( 2f );
				    
				    // Get point[] from database like form below
				    //draw line from point to point
				    float [] data1 = {80 * ratio1, 55 * ratio2, 75 * ratio1, 45 * ratio2,
				    		75 * ratio1, 45 * ratio2, 65 * ratio1, 40 * ratio2,
				    		65 * ratio1, 40 * ratio2, 55 * ratio1, 45 * ratio2,
				    		55 * ratio1, 45 * ratio2, 50 * ratio1, 55 * ratio2,
				    		50 * ratio1, 55 * ratio2, 50 * ratio1, 60 * ratio2, 
				    		50 * ratio1, 60 * ratio2, 65 * ratio1, 55 * ratio2,
				    		65 * ratio1, 55 * ratio2, 75 * ratio1, 60 * ratio2,
				    		75 * ratio1, 60 * ratio2, 80 * ratio1, 55 * ratio2};
				    float[] data2 = {90 * ratio1, 55 * ratio2, 85 * ratio1, 40 * ratio2, 
				    		85 * ratio1, 40 * ratio2, 75 * ratio1, 35 * ratio2,75 * ratio1, 35 * ratio2, 
				    		65 * ratio1, 30 * ratio2,65 * ratio1, 30 * ratio2, 60 * ratio1, 35 * ratio2,
				    		60 * ratio1, 35 * ratio2, 45 * ratio1, 35 * ratio2, 
				    		45 * ratio1, 35 * ratio2, 40 * ratio1, 55 * ratio2,
				    		40 * ratio1, 55 * ratio2, 40 * ratio1, 55 * ratio2,
				    		40 * ratio1, 55 * ratio2, 45 * ratio1, 70 * ratio2,
				    		45 * ratio1, 70 * ratio2, 65 * ratio1, 75 * ratio2,
				    		65 * ratio1, 75 * ratio2, 70 * ratio1, 75 * ratio2,
				    		70 * ratio1, 75 * ratio2, 75 * ratio1, 70 * ratio2,
				    		75 * ratio1, 70 * ratio2, 85 * ratio1, 65 * ratio2,
				    		85 * ratio1, 65 * ratio2, 90 * ratio1, 55 * ratio2,};
			//	    c.drawLines(data1, paint2);
			//	    c.drawLines(data2, paint3);
				    drawPoly(c, level1, data1);
				    drawPoly(c, level2, data2);
				    //set ImageBitmap to ImageView
				    picture.setImageBitmap( mutableBitmap );
				    //
			}});
	    loadbw.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Xét listview được chọn là ssid nào 
				//Lấy dữ liệu từ ssid đó đưa vào mảng data1 hoặc 2
//				APID = db.getAPIdByName(value);	
				
				res = getResources();
				    //Get image's size
				    BitmapFactory.Options dimensions = new BitmapFactory.Options(); 
				    dimensions.inJustDecodeBounds = true;
				    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.newmap, dimensions);
				    imgheight = dimensions.outHeight;
				    imgwidth =  dimensions.outWidth;
				    
				    //calculate the ratios between image's size and real world distance - how many pixels per meter
//				    String[] size = db.getMapsize(mapId);
//					ratio1 = (float)imgwidth/(Integer.parseInt(size[0]));
//				    ratio2 = (float)imgheight/(Integer.parseInt(size[1]));
				    ratio1 = (float)imgwidth/160;
				    ratio2 = (float)imgheight/100;
				    
				    //cast from immutableBitmap to mutableBitmap to insert into Canvas 
				    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.newmap );
				    Bitmap mutableBitmap = bm.copy(Bitmap.Config.ARGB_8888, true);
				    Canvas c = new Canvas( mutableBitmap );
				   
				    //set attributes for Paints (used to draw)
				    paint1.setColor( Color.BLUE );	    
				    paint2.setColor( Color.RED );
				    paint2.setStrokeWidth( 2f );   
				    paint3.setColor( Color.GREEN );
				    paint3.setStrokeWidth( 2f );
				    
				    // Get point[] from database like form below
				    //draw line from point to point
				    float [] data1 = {80 * ratio1, 55 * ratio2, 75 * ratio1, 45 * ratio2,
				    		75 * ratio1, 45 * ratio2, 65 * ratio1, 40 * ratio2,
				    		65 * ratio1, 40 * ratio2, 55 * ratio1, 45 * ratio2,
				    		55 * ratio1, 45 * ratio2, 50 * ratio1, 55 * ratio2,
				    		50 * ratio1, 55 * ratio2, 50 * ratio1, 60 * ratio2, 
				    		50 * ratio1, 60 * ratio2, 65 * ratio1, 55 * ratio2,
				    		65 * ratio1, 55 * ratio2, 75 * ratio1, 60 * ratio2,
				    		75 * ratio1, 60 * ratio2, 80 * ratio1, 55 * ratio2};
				    float[] data2 = {90 * ratio1, 55 * ratio2, 85 * ratio1, 40 * ratio2, 
				    		85 * ratio1, 40 * ratio2, 75 * ratio1, 35 * ratio2,75 * ratio1, 35 * ratio2, 
				    		65 * ratio1, 30 * ratio2,65 * ratio1, 30 * ratio2, 60 * ratio1, 35 * ratio2,
				    		60 * ratio1, 35 * ratio2, 45 * ratio1, 35 * ratio2, 
				    		45 * ratio1, 35 * ratio2, 40 * ratio1, 55 * ratio2,
				    		40 * ratio1, 55 * ratio2, 40 * ratio1, 55 * ratio2,
				    		40 * ratio1, 55 * ratio2, 45 * ratio1, 70 * ratio2,
				    		45 * ratio1, 70 * ratio2, 65 * ratio1, 75 * ratio2,
				    		65 * ratio1, 75 * ratio2, 70 * ratio1, 75 * ratio2,
				    		70 * ratio1, 75 * ratio2, 75 * ratio1, 70 * ratio2,
				    		75 * ratio1, 70 * ratio2, 85 * ratio1, 65 * ratio2,
				    		85 * ratio1, 65 * ratio2, 90 * ratio1, 55 * ratio2,};
			//	    c.drawLines(data1, paint2);
			//	    c.drawLines(data2, paint3);
				    drawPoly(c, level1, data1);
				    drawPoly(c, level2, data2);
				    //set ImageBitmap to ImageView
				    picture.setImageBitmap( mutableBitmap );
				    //
			}});
	     
	    save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// Luu ImageView thành file ảnh
				
			}
		});
	}
	//ham ve cho mot muc mau
	private void drawPoly(Canvas canvas, int color, float[] points) {
	    // line at minimum...
	    if (points.length < 2) {
	        return;
	    }
	    // paint
	    else{
		    Paint polyPaint = new Paint();
		    polyPaint.setColor(color);
		    polyPaint.setStyle(Paint.Style.FILL);
		    polyPaint.setStrokeWidth(5f);
		    polyPaint.setAlpha(0x40);
		    // path
		    Path polyPath = new Path();
		    polyPath.moveTo(points[0], points[1]);
		    int i, len;
		    len = points.length;
		    for (i = 2; i < len; i+=2) {
		        polyPath.lineTo(points[i], points[i+1]);
		    }
		    polyPath.lineTo(points[0], points[1]);
	
		    // draw
		    canvas.drawPath(polyPath, polyPaint);
	    }
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.wlanpicture, menu);
		return true;
	}

}
